package middleware

import (
	"math/rand"
	"strconv"
	"time"

	"github.com/gin-gonic/gin"
)

type Header struct {
	RequestId string `json:"requestId"`
}

// 公共参数处理
func SharedParamsMiddleware() gin.HandlerFunc {
	return func(c *gin.Context) {
		var header Header
		c.ShouldBindHeader(&header)
		if header.RequestId == "" {
			c.Set("requestId", strconv.Itoa(int(time.Now().UnixNano())+rand.Intn(100)))
		} else {
			c.Set("requestId", header.RequestId)
		}
		//添加相应头操作ID
		c.Writer.Header().Set("request-id", header.RequestId)
		c.Next()
	}
}
